import CryptoJS from 'crypto-js'
// import { JSEncrypt } from 'jsencrypt'
import JSEncrypt from 'jsencrypt'
import md5 from 'js-md5'

const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAh9v7hy1HhjwN gI4IgWG5Hfrxc/coCHE5KIJQ1ot3dZPq6500T+U/KNWK4q+zxkLoleD1Pw/2 /1G8LuWvWpu4ywIDAQABAkAzU/EbiQfUtyuCviaIexstMHm4jlAZoG0v2Mwh wl9HlCUVqQfjMXQjCj1ctaS35cU8ikMx2EDj9re3rmsQFMhBAiEAz90IOXVC kikBZNv+i8rDqJ5aKrZD/mRVw0dl7QvYY48CIQCnUkLz8fJrRyUfFspJV3MJ rr5LhpxMrpdvx+vrriJJBQIgAZeZ1obVabUys4kT3BJDiWkdG7Kltq8iEQrc 9wHeOHUCIDveqWpUVmOgb13yb6tSiS0bw6eOoPqnNjSfGoGYK1hpAiEAyBnt /NTKVi5bXscwdZk8VltRUzFgyRdB8wGNMj6vb24='
const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDK0UAjOVzbQSY5Io7X5GXRsI/tHG+uPcvFWZ7PrTD19pJ5jWQuBvxseZSTUL5syBTsX375vwOU5Cd96smVklCqhbuzMG70g6VqsCDqx/pkGESU+JwTSnnUQuJ4zpsZd2dulJXIfj6zb7R5TK3zNS7Ijow1GIzfr+2fHVBj4XvgWwIDAQAB'

/**
 * 随机生成字符串
 * @param len 长度
 * @param charSet 自定义字符集
 * @returns {string}
 */
const randomString = (len, charSet) => {
  charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  let randomString = ''
  for (let i = 0; i < len; i++) {
    const randomPoz = Math.floor(Math.random() * charSet.length)
    randomString += charSet.substring(randomPoz, randomPoz + 1)
  }
  return randomString
}
/**
 * 生成密钥
 * @param prefix
 * @returns {{iv: string, key: String}}
 */
const getKeyAndIv = (prefix) => {
  const key = md5.hex(prefix + randomString(10)) // '49fd50d4eebe22238d749fd50d4eebe1';//aes密钥
  const iv = 'c5543864ce12256b'// aes偏移量
  return {
    key: key,
    iv: iv
  }
}
/**
 * aes加密
 * @param key
 * @param iv
 * @param word
 * @returns string
 */
const aesEncrypt = (key, iv, word) => {
  const aesKey = CryptoJS.enc.Utf8.parse(key)
  const aesIv = CryptoJS.enc.Utf8.parse(iv)
  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, aesKey, {
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
    iv: aesIv
  })
  return encrypted.ciphertext.toString()
}
/**
 * aes解密
 * @param key
 * @param iv
 * @param word
 * @returns {string}
 */
const aesDecrypt = (key, iv, word) => {
  const aesKey = CryptoJS.enc.Utf8.parse(key)
  const aesIv = CryptoJS.enc.Utf8.parse(iv)
  const encryptedHexStr = CryptoJS.enc.Hex.parse(word)
  const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
  const decrypt = CryptoJS.AES.decrypt(srcs, aesKey, {
    iv: aesIv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  return decryptedStr.toString()
}

/**
 * res加密
 * @param param
 * @returns {string | false}
 */
const rsaEncrypt = (param) => {
  // 后台给的公钥
  const encryptor = new JSEncrypt() // 新建JSEncrypt对象
  encryptor.setPublicKey(publicKey) // 设置公钥
  return encryptor.encrypt(param) // 对密码进行加密
}

/**
 * res解密
 * @param word
 * @returns {string | false}
 */
const rsaDecrypt = (word) => {
  const decrypt = new JSEncrypt()
  decrypt.setPrivateKey(privateKey)
  return decrypt.decrypt(word)
}

/**
 * 加密参数
 * @param data 要加密的数据
 * @param prefix 密钥固定前缀
 * @returns {{data: (string|null), sign: (string|false), iv: string, key: String}}
 */
const encryptParam = (data, prefix) => {
  // 配置加密
  const keyAndIv = getKeyAndIv(prefix)
  const aes = {
    key: keyAndIv.key,
    iv: keyAndIv.iv
  }
  return {
    key: keyAndIv.key,
    iv: keyAndIv.iv,
    sign: rsaEncrypt(aes.key), // aes.key;///添加加密的aes key
    data: data ? aesEncrypt(keyAndIv.key, keyAndIv.iv, JSON.stringify(data)) : null
  }
}

export {
  aesEncrypt,
  aesDecrypt,
  rsaEncrypt,
  rsaDecrypt,
  getKeyAndIv,
  encryptParam
}

// WEBPACK FOOTER //
// ./src/utils/encrypt.js
